package com.seahorse.youliao.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.seahorse.youliao.service.SysUserRoleService;
import com.seahorse.youliao.service.entity.SysUserRoleDTO;
import com.seahorse.youliao.utils.BeanUtil;
import com.seahorse.youliao.vo.request.SysUserRoleQueryVO;
import com.seahorse.youliao.vo.response.SysUserRolePageInfoVO;
import com.seahorse.youliao.vo.response.SysUserRoleResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
* describe: 用户和角色关联表
* @author : songqiang
* @date: 2020-01-17 09:23:46.660
**/
@RestController
@Api("SysUserRoleController")
@RequestMapping("sysUserRole")
@Validated
public class SysUserRoleController {
	
    private static final Logger LOGGER = LoggerFactory.getLogger(SysUserRoleController.class);
    
	@Autowired
    private SysUserRoleService sysUserRoleService;
    /**
     * 分页查询
     * @param sysUserRoleQueryVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "分页查询")
    @PostMapping("getPageList")
    public SysUserRolePageInfoVO selectPageList(@RequestBody @Valid SysUserRoleQueryVO sysUserRoleQueryVO) {
        String info = String.format("The method name[selectPageList] params:%s", sysUserRoleQueryVO.toString());
        LOGGER.info(info);
        SysUserRoleDTO sysUserRole = BeanUtil.convert(sysUserRoleQueryVO, SysUserRoleDTO.class);

        Page<SysUserRoleResponseVO> page = PageHelper.startPage(sysUserRoleQueryVO.getPageNum(), sysUserRoleQueryVO.getPageSize());
        List<SysUserRoleDTO> sysUserRoleList = sysUserRoleService.getList(sysUserRole);
        SysUserRolePageInfoVO sysUserRolePageInfo = new SysUserRolePageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), sysUserRolePageInfo);
        List<SysUserRoleResponseVO> voList = BeanUtil.convert(sysUserRoleList, SysUserRoleResponseVO.class);
        sysUserRolePageInfo.setList(voList);

        return sysUserRolePageInfo;
    }


}
